import java.util.Scanner;

/**
 * @author vivi
 * @description
 * @date 2024/4/15
 */
public class Test {

    public static int process(String[] strs, String str1, String str2) {
        if (str1 == null || str2 == null) {
            return -1;
        }
        int last1 = -1, last2 = -1, min = Integer.MAX_VALUE;
        for (int i = 0; i < strs.length; i++) {
            if (strs[i].equals(str1)) {
                min = last2 == -1 ? min : Math.min(min, i - last2);
                last1 = i;
            } else if (strs[i].equals(str2)) {
                min = last1 == -1 ? min : Math.min(min, i - last1);
                last2 = i;
            }
        }
        return min == Integer.MAX_VALUE ? -1 : min;
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String str1 = sc.next();
        String str2 = sc.next();
        String[] strs = new String[n];
        for (int i = 0; i < n; i ++) {
            strs[i] = sc.next();
        }
        sc.close();
        int res = process(strs, str1, str2);
        System.out.println(res);
    }


    public static void main3 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        String word1 = scanner.nextLine();
        String word2 = scanner.nextLine();
        String words[] = new String [num];
        for(int i=0; i<words.length;i++){
            words[i] = scanner.next();
        }
        int minLength = findMinLength(words,word1,word2);
        System.out.println(minLength);
    }

    public static int findMinLength(String[] words, String word1, String word2){
        if (word1 == null || word2 == null) {
            return -1;
        }
        if (word1.equals(word2)) {
            return 0;
        }
        if(words.length < 1){
            return -1;
        }

        //存放word1和word2出现的位置
        int[] index1 = new int[words.length];
        int[] index2 = new int[words.length];
        int m = 0, n = 0;
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if(word.equals(word1)){
                index1[m++] = i;
            } else if (word.equals(word2)) {
                index2[n++] = i;
            }
        }

        int length = Integer.MAX_VALUE;

        for (int a = 0, b = 0; a < m && b < n; ) {
            int x = index1[a];
            int y = index2[b];

            length = Math.min(length,Math.abs(x-y));

            if(x < y){
                a++;
            }else{
                b++;
            }
        }

        return length;
    }

    public static void main2 (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] cost = new int[n];
        for (int i = 0; i < n; i++) {
            cost[i] = sc.nextInt();
        }
        System.out.println(minmumCost(n, cost));
    }

    private static int minmumCost(int n, int[] cost) {
        int[] minCost = new int[n];
        minCost[0] = 0;
        minCost[1] = Math.min(cost[0], cost[1]);
        for (int i = 2; i < n; i++) {
            minCost[i] = Math.min(minCost[i - 2] + cost[i - 1], minCost[i - 1] + cost[i]);
        }
        return minCost[n - 1];
    }

//    牛牛正在寄快递，他了解到
//    快递在1kg 以内的按起步价20元计算，超出部分按每kg 1元计算，不足1kg 部分按1kg计算。
//    如果加急的话要额外付五元，请问牛牛总共要支付多少快递费
    public static void main1 (String[] args) {
        Scanner scanner = new Scanner(System.in);
        double a = scanner.nextDouble();
        String c = scanner.next();
        char b = c.charAt(0);

        int money = 0;
        if(a <= 1){
            money = 20;
        }else {
            //向上取整函数:Math.ceil()
            money = (int) (20 + Math.ceil(a-1)*1);
        }
        if (b == 'y'){
            money += 5;
        }
        System.out.println(money);


//        System.out.println(a);
//        System.out.println(b);

    }
}
